/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import Pojos.JsonPojo;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

/**
 *
 * @author bugtraq
 */
public class Utils {

    public Object getJSON(String urlService) {
        return getJSON(urlService, null);
    }

    public Object getJSON(String urlService, List camposJson) {
        String variable = "";
        Object obj = null;
        boolean ban = camposJson == null || camposJson.isEmpty();
        List<JsonPojo> res = new ArrayList<JsonPojo>();
        List campos = new ArrayList();
        try {
            String token = "36bc11762f97f155729497f7099d76c4";
//            String url = "http://api.gobiernoabierto.gob.sv/institutions";
            URL u = new URL(urlService);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setRequestProperty("Content-length", "0");
            c.setUseCaches(false);
            c.setAllowUserInteraction(false);
            c.setRequestProperty("Authorization", "Token token=\"" + token + "\"");
            c.connect();
            int status = c.getResponseCode();

            switch (status) {
                case 200:
                case 201:
                    BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
                    StringBuilder sb = new StringBuilder();
                    String line;
                    while ((line = br.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    br.close();
//                    System.out.println(sb.toString());
                    variable = sb.toString();
//                    String[] var = {"accepts_online_information_requests"};
                    obj = JSONValue.parse(variable);
                    for (JSONObject actual : (List<JSONObject>) obj) {
//                        res.add(new JsonPojo(arrayToList(actual.values().toArray())));
                        if (!ban) {//muestra solo los elementos que están en la lista
                            JsonPojo elem = new JsonPojo();
                            for (String aux : (List<String>) camposJson) {
                                elem.setValue(actual.get(aux));
                                elem.setKey(aux);
                                campos.add(elem);
                                elem = new JsonPojo();
                            }
                        } else {//muestra todos los campos del registro
                            campos = arrayToList(actual.values().toArray());
                        }
                        res.add(new JsonPojo(campos));
                        campos = new ArrayList<JsonPojo>();
                    }
            }

        } catch (Exception e) {
//                Logger.getLogger(DebugServer.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }

    public List arrayToList(Object[] array) {
        List res = new ArrayList();
        for (Object actual : array) {
            res.add(actual);
        }
        return res;
    }
}
